package com.example.yichuangyun.mvvm.ui.adapter;

import static com.scwang.smartrefresh.layout.util.SmartUtil.dp2px;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemPlaceOrderBinding;
import com.example.yichuangyun.databinding.ItemPlaceOrderDetailBinding;
import com.example.yichuangyun.databinding.ItemProductAttrBinding;
import com.example.yichuangyun.http.ApiFactory;
import com.example.yichuangyun.model.order.ProductReqModel;
import com.example.yichuangyun.model.order.SelectOrderInfoDataModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.widget.flowLayout.FlowLayoutManager;
import com.example.yichuangyun.widget.flowLayout.SpaceItemDecoration;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@SuppressLint("NotifyDataSetChanged")
public class PlaceOrderAdapter extends BaseQuickAdapter<SelectOrderInfoDataModel.OrderProductModel, DataBindingHolder<ItemPlaceOrderBinding>> {

    private static AttrListener mAttrListener;
    private final Context context;
    private final DecimalFormat decimalFormat;

    public PlaceOrderAdapter(Context context) {
        this.context = context;
        decimalFormat = new DecimalFormat(".00");
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemPlaceOrderBinding> bindingHolder, int i, @Nullable SelectOrderInfoDataModel.OrderProductModel orderProductModel) {
        ItemPlaceOrderBinding binding = bindingHolder.getBinding();
        binding.setMOrderProductModel(orderProductModel);
        if (orderProductModel != null) {
            if (orderProductModel.getPrice() == 0) {
                binding.tvPrice.setText(context.getString(R.string.total_price, String.valueOf(0)));
            } else if (orderProductModel.getPrice() > 0) {
                binding.tvPrice.setText(context.getString(R.string.total_price, decimalFormat.format(orderProductModel.getPrice())));
            } else {
                binding.tvPrice.setText(context.getString(R.string.no_price));
            }
            Glide.with(context).load(ApiFactory.PRODUCT_URL + orderProductModel.getPcBanner()).into(binding.ivProduct);
            ServiceAdapter serviceAdapter = new ServiceAdapter(getItems());
            binding.recyclerView.setAdapter(serviceAdapter);
            serviceAdapter.submitList(orderProductModel.getServiceList());
            binding.llProduct.setOnClickListener(view -> {
                orderProductModel.setExpand(!orderProductModel.isExpand());
                binding.recyclerView.setVisibility(orderProductModel.isExpand() ? View.VISIBLE : View.GONE);
                binding.line.setVisibility(orderProductModel.isExpand() ? View.VISIBLE : View.GONE);
                binding.ivExpand.setImageResource(orderProductModel.isExpand() ? R.mipmap.icon_down : R.mipmap.icon_up);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemPlaceOrderBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_place_order, viewGroup);
    }

    private static class ServiceAdapter extends BaseQuickAdapter<SelectOrderInfoDataModel.OrderServiceModel, DataBindingHolder<ItemProductAttrBinding>> {

        private final List<SelectOrderInfoDataModel.OrderProductModel> productList;

        public ServiceAdapter(List<SelectOrderInfoDataModel.OrderProductModel> productList) {
            this.productList = productList;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemProductAttrBinding> bindingHolder, int i, @Nullable SelectOrderInfoDataModel.OrderServiceModel orderServiceModel) {
            ItemProductAttrBinding binding = bindingHolder.getBinding();
            if (orderServiceModel != null) {
                binding.tvTitle.setText(orderServiceModel.getServiceName());
                binding.tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                AttrsAdapter attrsAdapter = new AttrsAdapter(productList, getItem(i));
                binding.recyclerView.setAdapter(attrsAdapter);
                attrsAdapter.submitList(orderServiceModel.getAttrs());
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemProductAttrBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_product_attr, viewGroup);
        }
    }

    private static class AttrsAdapter extends BaseQuickAdapter<SelectOrderInfoDataModel.OrderAttrsModel, DataBindingHolder<ItemProductAttrBinding>> {

        private final List<SelectOrderInfoDataModel.OrderProductModel> productList;
        private final SelectOrderInfoDataModel.OrderServiceModel serviceModel;

        public AttrsAdapter(List<SelectOrderInfoDataModel.OrderProductModel> productList, SelectOrderInfoDataModel.OrderServiceModel serviceModel) {
            this.productList = productList;
            this.serviceModel = serviceModel;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemProductAttrBinding> bindingHolder, int i, @Nullable SelectOrderInfoDataModel.OrderAttrsModel orderAttrsModel) {
            ItemProductAttrBinding binding = bindingHolder.getBinding();
            if (orderAttrsModel != null) {
                binding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                binding.tvTitle.setTextColor(0xFF7C7C7C);
                binding.tvTitle.setText(orderAttrsModel.getAttName());
                binding.tvTitle.setPadding(0, 0, 0, dp2px(5));
                final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
                if (bindingHolder.itemView.getTag() == null) {
                    binding.recyclerView.addItemDecoration(new SpaceItemDecoration(dp2px(5)));
                    bindingHolder.itemView.setTag("item");
                }
                binding.recyclerView.setLayoutManager(flowLayoutManager);
                MsgAdapter msgAdapter = new MsgAdapter(productList, orderAttrsModel.getMsgs(), serviceModel);
                binding.recyclerView.setAdapter(msgAdapter);
                msgAdapter.submitList(orderAttrsModel.getMsgs());
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemProductAttrBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_product_attr, viewGroup);
        }
    }

    private static class MsgAdapter extends BaseQuickAdapter<SelectOrderInfoDataModel.OrderMsgModel, DataBindingHolder<ItemPlaceOrderDetailBinding>> {

        private final List<SelectOrderInfoDataModel.OrderProductModel> productList;
        private final List<SelectOrderInfoDataModel.OrderMsgModel> mData;
        private final SelectOrderInfoDataModel.OrderServiceModel serviceModel;

        public MsgAdapter(List<SelectOrderInfoDataModel.OrderProductModel> productList, List<SelectOrderInfoDataModel.OrderMsgModel> items,
                          SelectOrderInfoDataModel.OrderServiceModel serviceModel) {
            this.productList = productList;
            this.mData = items;
            this.serviceModel = serviceModel;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemPlaceOrderDetailBinding> bindingHolder, int i, @Nullable SelectOrderInfoDataModel.OrderMsgModel orderMsgModel) {
            ItemPlaceOrderDetailBinding binding = bindingHolder.getBinding();
            if (orderMsgModel != null) {
                binding.tvTitle.setText(CommonUtil.isNumber(orderMsgModel.getAttrMsg()) ? orderMsgModel.getAttrMsg()
                        + (serviceModel.getServicePeriod() == 12 ? "个月" : "年") : CommonUtil.checkText(orderMsgModel.getAttrMsg()));
                binding.tvTitle.setTextColor(orderMsgModel.isSelect() ? 0xFF426FFF : 0xFF4A4A4A);
                binding.tvTitle.setBackgroundResource(orderMsgModel.isSelect() ? R.drawable.shape_label_bg_sel : R.drawable.shape_label_bg_def);
                binding.tvTitle.setOnClickListener(view -> {
                    if (!orderMsgModel.isSelect()) {
                        for (SelectOrderInfoDataModel.OrderMsgModel msgModel : mData) {
                            msgModel.setSelect(false);
                        }
                        orderMsgModel.setSelect(true);

                        for (SelectOrderInfoDataModel.OrderProductModel productModel : productList) {
                            ProductReqModel reqModel = new ProductReqModel();
                            reqModel.setProductId(productModel.getId());
                            List<SelectOrderInfoDataModel.OrderServiceModel> serviceList = productModel.getServiceList();
                            if (serviceList != null) {
                                List<ProductReqModel.ProductServiceModel> selServiceList = new ArrayList<>();
                                for (SelectOrderInfoDataModel.OrderServiceModel orderServiceModel : serviceList) {
                                    ProductReqModel.ProductServiceModel productServiceModel = new ProductReqModel.ProductServiceModel();
                                    productServiceModel.setServiceName(orderServiceModel.getServiceName());
                                    productServiceModel.setServiceId(orderServiceModel.getId());
                                    selServiceList.add(productServiceModel);
                                    reqModel.setProductReqs(selServiceList);
                                    List<SelectOrderInfoDataModel.OrderAttrsModel> attrsList = orderServiceModel.getAttrs();
                                    if (attrsList != null) {
                                        List<String> remarkList = new ArrayList<>();
                                        for (SelectOrderInfoDataModel.OrderAttrsModel attrsModel : attrsList) {
                                            List<SelectOrderInfoDataModel.OrderMsgModel> msgList = attrsModel.getMsgs();
                                            if (msgList != null) {
                                                for (SelectOrderInfoDataModel.OrderMsgModel msgModel : msgList) {
                                                    if (msgModel.isSelect()) {
                                                        if (CommonUtil.isNumber(msgModel.getAttrMsg())) {
                                                            productServiceModel.setServiceAttrCycle(msgModel.getAttrMsg());
                                                        } else {
                                                            remarkList.add(msgModel.getAttrMsg());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        productServiceModel.setRemarks(remarkList);
                                    }
                                }
                            }
                            mAttrListener.onAttrListener(reqModel);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemPlaceOrderDetailBinding> onCreateViewHolder
                (@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_place_order_detail, viewGroup);
        }
    }

    public void setAttrListener(AttrListener attrListener) {
        mAttrListener = attrListener;
    }

    public interface AttrListener {
        void onAttrListener(ProductReqModel reqModel);
    }
}
